package com.example.demo.test.lecture.httpstest;


import com.alibaba.csb.sdk.HttpReturn;
import com.example.demo.test.lecture.httpstest.ContentBody.Type;
import org.apache.http.Header;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class DiagnosticHelper {
    public static final String DIAGNOSTIC_REQUEST_HEADERS = "requestHeaders";
    public static final String DIAGNOSTIC_SIGN_PARAMS = "signParams";
    public static final String DIAGNOSTIC_START_TIME = "startTime";
    public static final String DIAGNOSTIC_END_TIME = "endTime";
    public static final String DIAGNOSTIC_INVOKE_TIME = "totalInvokeTime";
    public static final String DIAGNOSTIC_REQUEST_SIZE = "requestSize";
    public static final String DIAGNOSTIC_RESPONSE_SIZE = "responseSize";
    public static final String DIAGNOSTIC_BROKER_IP = "brokerIp";

    public DiagnosticHelper() {
    }

    public static void calcResponseSize(HttpReturn ret) {
        if (ret != null && ret.diagnosticFlag) {
            long size = 0L;
            if (ret.response != null) {
                ret.diagnosticInfo.put("responseSize", String.valueOf(ret.response.length()));
            }

        }
    }

    public static void calcRequestSize(HttpReturn ret, String requestURL, Map<String, String> paramsMap, ContentBody cb) {
        if (ret != null && ret.diagnosticFlag) {
            long size = 0L;
            if (requestURL != null) {
                size += (long)requestURL.length();
            }

            if (paramsMap != null) {
                Iterator var6 = paramsMap.entrySet().iterator();

                while(var6.hasNext()) {
                    Entry<String, String> kv = (Entry)var6.next();
                    if (kv.getKey() != null) {
                        size += (long)((String)kv.getKey()).length();
                    }

                    if (kv.getValue() != null) {
                        size += (long)((String)kv.getValue()).length();
                    }
                }
            }

            if (cb != null && cb.getContentBody() != null) {
                if (cb.getContentType() == Type.BINARY) {
                    size += (long)((byte[])((byte[])cb.getContentBody())).length;
                } else {
                    size += (long)((String)cb.getContentBody()).length();
                }
            }

            ret.diagnosticInfo.put("requestSize", String.valueOf(size));
        }
    }

    public static void setStartTime(HttpReturn ret, long startT) {
        if (ret != null && ret.diagnosticFlag) {
            ret.diagnosticInfo.put("startTime", String.valueOf(startT));
        }
    }

    public static void setInvokeTime(HttpReturn ret, long invokeTime) {
        if (ret != null && ret.diagnosticFlag) {
            ret.diagnosticInfo.put("totalInvokeTime", String.valueOf(invokeTime));
        }
    }

    public static void setEndTime(HttpReturn ret, long endTime) {
        if (ret != null && ret.diagnosticFlag) {
            ret.diagnosticInfo.put("endTime", String.valueOf(endTime));
        }
    }

    public static void setRequestHeaders(HttpReturn ret, Header[] allHeaders) {
        if (ret != null && ret.diagnosticFlag) {
            if (allHeaders != null && allHeaders.length > 0) {
                StringBuffer sb = new StringBuffer();
                Header[] var3 = allHeaders;
                int var4 = allHeaders.length;

                for(int var5 = 0; var5 < var4; ++var5) {
                    Header h = var3[var5];
                    if (sb.length() > 0) {
                        sb.append(", ");
                    }

                    sb.append(h.getName()).append(":").append(h.getValue());
                }

                ret.diagnosticInfo.put("requestHeaders", sb.toString());
            }

        }
    }

    public static void setSignDiagnosticInfo(HttpReturn ret, StringBuffer signDiagnosticInfo) {
        if (ret != null && ret.diagnosticFlag) {
            if (signDiagnosticInfo != null) {
                ret.diagnosticInfo.put("signParams", signDiagnosticInfo.toString());
            }

        }
    }

    public static StringBuffer getSignDiagnosticInfo(HttpReturn ret) {
        return ret != null && ret.diagnosticFlag ? new StringBuffer() : null;
    }
}
